<?php
/*
用户搜索类
time:2012-12-15
author:jackBrown
version:1.0.8
*/
if(!defined('IN_WEB')){
	exit('Access Deny!');
}

class search{
	
	/*排序,字段键值=>排序类型*/
	public $order = array();
	
	/*条件*/
	public $condition = array();
	
	public $source;
	public $url;
	public $isNew = false;
	public $orderURL = array();
	
	public function __construct($s){
		if($s == null ){
			return false;
		}
		
		$this->source = $s;
		
	}
	
	/**
	 * 组建search相关信息
	 */
	public function buildSearch(){
		
		$temp = '';
		$url = '';
		if($this->isNew == false){
			$this->buildSearchFromReq();
		}
		
		if($this->condition!=null && is_array($this->condition)){
			
			$temp = !strstr($this->source,'where') ? ' where ' : $temp .' and (';
			$url  = !preg_match('/\?.+/',$this->url) ? '?' : '&';
			
			foreach($this->condition as $f=>$c){
				
				if(is_array($c) && $c!=null){
					
					$temp.='(';
					$url.= 'fld_'.str_replace('.',':',$f).'_cond=[';
					foreach($c as $e){
						
						$temp.=" $f$e and";
						$url.= urlencode($e).":";
						
					}
					
					$url = stripLast($url,':');
					$url.= ']&';
					$temp =stripLast($temp,'and');
					$temp.=') or';
					
				}else{
					$temp.=" $f$c or";
					$f = str_replace('.',':',$f);
					$url.= 'fld_'.$f.'_cond='.urlencode($c).'&';
				}
			}
			
			$temp = stripLast($temp,'or');
			$temp = !strstr($this->source,'where') ? $temp : $temp .' ) ';
			$url  = stripLast($url,'&');
										
		}
		
		if($this->groupBy!=null){
			$temp.= ' group by '.$this->groupBy;
		}
		
		if($this->order!=null && is_array($this->order)){
			
			$temp.=' order by ';	
			if($url!=''){
				$url.='&';
			}else{
				$url  = !preg_match('/\?.+/',$this->url) ? $url.'?' : $url.'&';	
			}
			
			$orderURL = array();
			$orderTemp = $url;

			foreach($this->order as $f=>$o){
				
				$o = setDefault($o,'asc',array('asc','desc'));
				$temp.="$f $o,";
				$f = str_replace('.',':',$f);
				$orderURL[$f] = $this->url.$orderTemp.'fld_'.$f.'_order='.$this->notOrder($o);
				$url.= 'fld_'.$f.'_order='.$o.'&';
				$GLOBALS['_GET']['fld_'.$f.'_order'] = $o;
			}
			
			$temp = stripLast($temp,',');
			$url  = stripLast($url,'&');
		}
		
		$this->source = $this->source.$temp;
		$this->orderURL = $orderURL;
		$this->url    .= $url;
		
	}
	
	/**
	*添加固定条件,不加入地址中
	*@param $key字段
	*@param $val 条件值
	*@param $logic逻辑关系
	*/
	public function addFixCond($key,$val,$logic){
		
		if(!in_array($logic,array('and','or'))){
			return false;
		}
		
		
		if(!strstr($this->source,'where')){
			$temp =" where $key$val ";
		}else{
			$temp =" $logic $key$val ";	
		}
		
		$this->source =  $this->source.$temp;
		
	}
	
	/**
	 * 过滤搜索时会带来安全问题的字符
	 * @param string $kwyword
	 * @return string
	 */
	public function toSafeKeyword($keyword){
		
		return preg_replace('/\s|\'|\"/','',$keyword);
	}
	
	/**
	 * 增加时间区间条件
	 * @param string $key 字段名
	 * @param string $stime 开始时间
	 * @param string $etime 结束时间
	 *  
	 */
	public function addTimeArea($key,$stime,$etime){
		
		if($stime=='' && $stime==''){
			return false;
		}
		
		/*时间区间*/
		if($stime && $etime==''){
		
			$this->addFixCond("unix_timestamp($key)", ">=unix_timestamp('".date('Y-m-d',strtotime($stime))."')",'and');
			
		}else if($stime=='' && $etime!=''){
		
			$this->addFixCond("unix_timestamp($key)","<=unix_timestamp('".date('Y-m-d',strtotime($etime))."')",'and');
			
		}else if($stime!='' && $etime!=''){
			$this->addFixCond(' ',"(unix_timestamp($key)<=unix_timestamp('".date('Y-m-d',strtotime($etime))."') and unix_timestamp($key)>=unix_timestamp('".date('Y-m-d',strtotime($stime))."'))",'and');
		}
		
	}
	
	/**
	 * 增加 like 条件
	 * @param string $key字段名
	 * @param string $keyword关键词
	 * 
	 */
	public function addKwdLike($key,$keyword){
	
		$keyword = $this->toSafeKeyword($keyword);
		$this->condition[$key] = " like '%$keyword%' ";
	}
	
	/**
	 * 增加 = 条件
	 * @param string $key字段名
	 * @param string $keyword关键词
	 * 
	 */
	public function addKwdEq($key,$keyword){
	
		$keyword = $this->toSafeKeyword($keyword);
		$this->condition[$key] = " ='$keyword'";
	}
		
	/**
	 * 增加>=条件
	 * @param string $key字段名
	 * @param string $keyword关键词
	 * 
	 */
	public function addKwdMaxEq($key,$keyword){
	
		$keyword = $this->toSafeKeyword($keyword);
		$this->condition[$key] = ">=$keyword";
	}
	
	/**
	 * 增加>条件
	 * @param string $key字段名
	 * @param string $keyword关键词
	 * 
	 */
	public function addKwdMax($key,$keyword){
	
		$keyword = $this->toSafeKeyword($keyword);
		$this->condition[$key] = ">$keyword";
	}
	
	/**
	 * 增加<=条件
	 * @param string $key字段名
	 * @param string $keyword关键词
	 * 
	 */
	public function addKwdMinEq($key,$keyword){
	
		$keyword = $this->toSafeKeyword($keyword);
		$this->condition[$key] = "<=$keyword";
	}	
	
	/**
	 * 增加<条件
	 * @param string $key字段名
	 * @param string $keyword关键词
	 * 
	 */
	public function addKwdMin($key,$keyword){
	
		$keyword = $this->toSafeKeyword($keyword);
		$this->condition[$key] = "<$keyword";
	}

	/**
	 * 增加!=条件
	 * @param string $key字段名
	 * @param string $keyword关键词
	 * 
	 */
	public function addKwdNotEq($key,$keyword){
	
		$keyword = $this->toSafeKeyword($keyword);
		$this->condition[$key] = "!=$keyword";
	}
	
	
	
	/**
	 * 增加 in()条件
	 * @param string $key字段名称
	 * @param array/string $items 值列表
	 */
	public function createIn($key,$items){
		if($items==null || $items==''){
			return false;
		}
		
		if(is_array($items)){
		
			foreach($items as $k=>$val){
				$items[$k] = addCharLR($this->toSafeKeyWord($val),"'");
			}
			
			$cond = implode(',',$items);
			$this->addFixCond($key," in ($cond) ",'and');
		}else{
			$this->addFixCond($key,"=($items)",'and');
		}
	}
	
	/**
	*从地址中获取相关条件
	*当为条件时:号为多个条件的分隔点
	*/
	protected function buildSearchFromReq(){
		
		if($_REQUEST!=null){
			
			$orderArray = array();
			
			foreach($_REQUEST as $g=>$getVal){
			
				$item = $this->getFldInfor($g);
				if($item['fld']!=null){
					
					if($item['type'] == 'cond'){
						
						if(!preg_match('/\[.+:.+\]/',$getVal,$m)){
							$this->condition[$item['fld']] = stripslashes($getVal);
						}else{
							
							$temp = substr($getVal,1,-1);
							$val = explode(':',$temp);
							$this->condition[$item['fld']] = array(stripslashes($val[0]),stripslashes($val[1]));
						}
					}else if($item['type'] == 'order'){
					
						$orderArray[$item['fld']] = $getVal;
						unset($this->order[$item['fld']]);
					}
				}
			}
			
			/*调速排序顺序*/
			if($orderArray != null){
				
				if($this->order!=null){
					foreach($this->order as $k=>$o){
						$orderArray[$k] = $o;
									
					}
				}
				
				$this->order = $orderArray;
				unset($orderArray);
				
			}
		}
		
	}
	

	/**
	*读取地址信息
	*@param string $k 由地址参数的键值
	*/
	protected function getFldInfor($k){
		
		if(preg_match('/fld_([a-z_:\(\)]+)_(cond|order)/i',$k,$m)){
			
			$m[1] = str_replace(':','.',$m[1]);
			return array('fld'=>$m[1],'type'=>$m[2]);
		}
		
		return null;
	}
	
	/**
	 * 获取排序的相反值 desc/asc
	 */
	private function notOrder($o){
		
		if($o == 'desc'){
			
			$o = 'asc';
		}else if($o == 'asc'){
			
			$o = 'desc';
		}
		
		return $o;
	}
}


?>